package com.devunion.bogdan.view;

import com.devunion.bogdan.view.actions.ActionManager;
import com.devunion.bogdan.model.project.ProjectManager;
import com.devunion.bogdan.model.project.ProjectSavingException;

import org.apache.log4j.Logger;

/**
 * @author Viktar Liaskovich
 */
public class AppLauncher {
    private static Logger log = Logger.getLogger(AppLauncher.class);
    private LibrarianEditor window;


    public AppLauncher() {
        initManagers();

        window = LibrarianEditor.getInstance();
    }

    public void startApp() {

        ActionManager.getInstance().update(false);

        window.showWindow();
    }

    private void initManagers() {
//        ProjectTreeBrowser.init();
    }

    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new ConfigUpdaterHook());

        new AppLauncher().startApp();
    }

    private static class ConfigUpdaterHook extends Thread {
        public void run() {
            try {
                if (ProjectManager.getInstance().isProjectOpened()) {
                    ProjectManager.getInstance().closeProject();
                }
            } catch (ProjectSavingException e) {
                log.debug(e.getMessage(), e);
            }
        }
    }
}
